<?php
namespace app\admin\model;
use think\Db;
use think\Model;

class FrDriverModel extends Model
{
	 public function __construct()
	{
		parent::__construct();

	}
	public function getDriverList($page,$limit,$map=array(),$field = '*')
    {
		 $list = Db::table('fr_driver_base d')->join(['fr_driver_info'=>'i'], 'i.driver_uid = d.driver_uid', 'LEFT')->join(['xm_user'=>'u'],'u.uid=d.driver_uid','LEFT')
            ->page($page,$limit)->where($map)->field($field)->order('d.id desc')->select();
        $orderInfo = [];
		 foreach ($list as $key=>$value){
             //驾驶证有效期
             if($value['valid_time'] >= time()){
                 $list[$key]['validTypeName'] = '<span style="color:green;">有效</span>';
             }else{
                 $list[$key]['validTypeName'] = '<span style="color:red;">已过期</span>';
             }
             //累计收入 实际收入 - 平台费
             $orderInfo = self::getFrOrderInfo($value['driver_uid']);
             if(!empty($orderInfo['sum_price']) && $orderInfo['sum_price'] > 0){
                 $list[$key]['sum_price'] = $orderInfo['sum_price']/100;
             }else{
                 $list[$key]['sum_price'] = 0;
             }
             $list[$key]['userCancelOrder']     = $orderInfo['userCancelOrder'];
             $list[$key]['driverCancelOrder']   = $orderInfo['driverCancelOrder'];
         }
        return $list;
	}

	public function getFrOrderInfo($driver_uid = ''){
	    //司机累计收入
        $orderInfo['sum_price'] = Db::table('fr_order_common')->where(['driver_uid'=>$driver_uid,'order_status'=>['>=',45],'is_pay'=>1,'is_del'=>2])->sum('need_price-platform_price');
        //乘客取消订单数量
        $orderInfo['userCancelOrder'] = Db::table('fr_order_common')->where(['driver_uid'=>$driver_uid,'order_status'=>10,'is_del'=>2])->field('common_id')->count();
        //司机取消订单数量
        $orderInfo['driverCancelOrder'] = Db::table('fr_order_common')->where(['driver_uid'=>$driver_uid,'order_status'=>5,'is_del'=>2])->field('common_id')->count();
        return $orderInfo;
    }


	public function getDriverInfo($did,$string='a.*')
	{
		return Db::table("fr_driver_base d")
            ->join(['fr_driver_info'=>'i'], 'i.driver_uid = d.driver_uid')
			->join(['fr_car'=>'c'],'c.driver_uid=d.driver_uid','LEFT')
			->field($string)
			->where(array('d.driver_uid'=>$did))->find();
	}

	public function getDriverMoneyList($page,$limit,$map=array(),$field = 'a.*')
    {
		return Db::name('driver_money a')->join('__DRIVER__ b','a.driver_id = b.driver_id','LEFT')
			->page($page,$limit)->where($map)->field($field)->order('id desc')->select();
	}

	public function searchDriver($map,$field='*',$flag=true)
	{
		return $flag ? Db::name('driver')->where($map)->field($field)->select()
			: Db::name('driver')->where($map)->column($field);
	}

}
